<?php
namespace app\beautiful\controller\admin\setting;
use app\beautiful\model\setting\SettingBalanceModel;
use app\BaseController;

class Balance extends BaseController{
    public function show()
    {
        if(null === $balance_setting=SettingBalanceModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $balance_setting = new SettingBalanceModel();
            $balance_setting->shop_id=$this->request->shopId;
            $balance_setting->save();
        }
        $taocan_detail=$balance_setting->taocan_detail;
        foreach ($taocan_detail as $k => $v){
            if(!isset($taocan_detail[$k]['once_card'])){
                $taocan_detail[$k]['once_card']=[];
            }
        }
        $detail=[
            'integral_deduction_balance_limit_rate'=>$balance_setting->integral_deduction_balance_limit_rate,
            'taocan_detail'=>$taocan_detail,
            'is_frozen_give_money'=>$balance_setting->is_frozen_give_money,
            'frozen_days'=>$balance_setting->frozen_days,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $integral_deduction_balance_limit_rate=$this->request->param('integral_deduction_balance_limit_rate/f',0);
        $taocan_detail=$this->request->param('taocan_detail','','security_json_or_array');
        $is_frozen_give_money=$this->request->param('is_frozen_give_money/d',0);
        $frozen_days=$this->request->param('frozen_days/d',0);

        if($integral_deduction_balance_limit_rate < 0) return $this->error('积分抵扣余额上限比不能小于0');
        if($integral_deduction_balance_limit_rate > 100) return $this->error('积分抵扣余额上限比不能大于100');
        //if(empty($taocan_detail)) return $this->error('余额充值套餐设置不能为空');
        if(!in_array($is_frozen_give_money,[0,1])) return $this->error('参数错误');

        if($is_frozen_give_money==1){
            if($frozen_days<=0)  return $this->error('冻结时间不能小于等于0');
        }

        foreach ($taocan_detail as $k => $v){
            if(empty($v['recharge_price']) || $v['recharge_price']<=0)
                return $this->error('第'.($k+1).'行的充值金额不能为空且不能小于0');

            if($k != 0){
                if($v['recharge_price']<=$taocan_detail[$k-1]['recharge_price'])
                    return $this->error('第'.($k+1).'行的充值金额不能小于等于第'.($k).'行的充值金额');
            }
            $taocan_detail[$k]['recharge_price']=(int)$v['recharge_price'];
            $taocan_detail[$k]['give_price']=(int)$v['give_price'];
            if(!empty($v['coupon'])){
                foreach ($v['coupon'] as $key=>$coupon_id){
                    $taocan_detail[$k]['coupon'][$key]=(int)$coupon_id;
                }
            }

            if(!empty($v['once_card'])){
                foreach ($v['once_card'] as $key=>$once_card_id){
                    $taocan_detail[$k]['once_card'][$key]=(int)$once_card_id;
                }
            }
        }

        if(null === $balance_setting=SettingBalanceModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $balance_setting = new SettingBalanceModel();
            $balance_setting->shop_id=$this->request->shopId;
        }

        $balance_setting->integral_deduction_balance_limit_rate=$integral_deduction_balance_limit_rate;
        $balance_setting->taocan_detail=$taocan_detail;
        $balance_setting->is_frozen_give_money=$is_frozen_give_money;
        $balance_setting->frozen_days=$frozen_days;
        $balance_setting->save();

        return $this->success('保存成功');
    }
}
